package sxt.service.impl;

import sxt.mapper.AnnouncementMapper;
import sxt.entity.Announcement;
import sxt.service.AnnouncementService;
import sxt.util.DBUtil;

import java.util.Date;
import java.util.List;

public class AnnouncementServiceImpl implements AnnouncementService {
    @Override
    public List<Announcement> findAllComment() {
        //1.DBUtil创建session会话,获取mapper对象
        AnnouncementMapper mapper = DBUtil.getSqlSession().getMapper(AnnouncementMapper.class);

        //2.通用mapper对象获取数据
        List<Announcement> list = mapper.selectAll();

        //3.DBUtil关闭session会话
        DBUtil.closeAll();

        //4.将数据返回给该方法的调用者
        return list;
    }

    @Override
    public int addAnnocement(String announcement, Date date) {
        //1.DBUtil创建session会话,获取mapper对象
        AnnouncementMapper mapper = DBUtil.getSqlSession().getMapper(AnnouncementMapper.class);

        //2.通用mapper对象获取数据
        int insert = mapper.insertAnnocement(announcement,date);

        //3.DBUtil关闭session会话
        DBUtil.closeAll();

        //4.将数据返回给该方法的调用者
        return insert;
    }

    @Override
    public Announcement findOneAnnouncement(int id) {
        //1.DBUtil创建session会话,获取mapper对象
        AnnouncementMapper mapper = DBUtil.getSqlSession().getMapper(AnnouncementMapper.class);

        //2.通用mapper对象获取数据
        Announcement oneAnnouncement = mapper.selectOneAnnouncement(id);

        //3.DBUtil关闭session会话
        DBUtil.closeAll();

        //4.将数据返回给该方法的调用者
        return oneAnnouncement;
    }

    @Override
    public int changeAnnouncement(int id, String announcement) {
        //1.DBUtil创建session会话,获取mapper对象
        AnnouncementMapper mapper = DBUtil.getSqlSession().getMapper(AnnouncementMapper.class);

        //2.通用mapper对象获取数据
        int update = mapper.updateAnnouncement(id,announcement);

        //3.DBUtil关闭session会话
        DBUtil.closeAll();

        //4.将数据返回给该方法的调用者
        return update;
    }

    @Override
    public int removeAnnouncement(int id) {
        //1.DBUtil创建session会话,获取mapper对象
        AnnouncementMapper mapper = DBUtil.getSqlSession().getMapper(AnnouncementMapper.class);

        //2.通用mapper对象获取数据
        int deleteAnnouncement = mapper.deleteAnnouncement(id);

        //3.DBUtil关闭session会话
        DBUtil.closeAll();

        //4.将数据返回给该方法的调用者
        return deleteAnnouncement;
    }
}
